Passed
Pull Request — master (#103)
by Mark
01:28
created

Relation.setFillPropertyOnParent   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
dl 0
loc 13
rs 9.8
c 0
b 0
f 0
1
import Field from "./Field";
2
import ForeignKey from "./Field/ForeignKey";
3
import {ModelStaticInterface, TableInterface} from "../../JeloquentInterfaces";
4
5
/**
6
 *
7
 */
8
export default class Relation extends Field {
9
10
    foreignKey: string;
11
12
    model: ModelStaticInterface;
13
14
    constructor(model: ModelStaticInterface, foreignKey: string = null, name: string = null) {
15
        const className = name ?? model.snakeCaseClassName;
16
        super(className);
17
        this.model = model;
18
        this.foreignKey = foreignKey;
19
    }
20
21
    getRelationalFields(): Array<ForeignKey> {
22
        return [new ForeignKey(this.foreignKey).setRelation(this)];
23
    }
24
25
    tableSetup(table: TableInterface): void {
26
        table.registerIndex(this.foreignKey);
27
    }
28
29
    set _value(value) {
30
        if (!Array.isArray(value)) {
31
            value = [value];
32
        }
33
        value.forEach((modelValue) => {
34
            if (!(this.model.ids().includes(modelValue.id))) {
35
                this.model.insert(modelValue);
36
            }
37
        });
38
    }
39
}